/*
 * @(#) bt.java   98/05/14
 * Copyright (c) 1998 UW.  All Rights Reserved.
 *        Author Xiaohu Li (xiaohu@cs.wisc.edu)
 */
package btree;
import global.*;

/** KeyDataEntry: define (key, data) pair.
 */
public class KeyDataEntry {
   /** key in the (key, data)
    */  
   public KeyClass key;
   /** data in the (key, data)
    */
   public DataClass data;
   
  /** Class constructor
   */
  public KeyDataEntry( Integer key, PageId pageNo) {
     this.key = new IntegerKey(key); 
     this.data = new IndexData(pageNo);
  }; 

  /** Class constructor
   */
  public KeyDataEntry( double key, PageId pageNo) {
     this.key = new DoubleKey(key); 
     this.data = new IndexData(pageNo);
  }; 

  /** Class constructor.
   */
  public KeyDataEntry( KeyClass key, PageId pageNo) {

     data = new IndexData(pageNo); 
     if ( key instanceof IntegerKey ) 
        this.key= new IntegerKey(((IntegerKey)key).getKey());
     else if ( key instanceof StringKey ) 
        this.key= new StringKey(((StringKey)key).getKey());   
     else if (key instanceof DoubleKey)
    	this.key= new DoubleKey(((DoubleKey)key).getKey());  
  };


  /** Class constructor.
   */
  public KeyDataEntry( String key, PageId pageNo) {
     this.key = new StringKey(key); 
     this.data = new IndexData(pageNo);
  };

  /** Class constructor.
   */
  public KeyDataEntry( Integer key, TID tid) {
     this.key = new IntegerKey(key); 
     this.data = new LeafData(tid);
  };
  
  public KeyDataEntry( Integer key, LID lid) {
	     this.key = new IntegerKey(key); 
	     this.data = new LeafDataL(lid);
	  };
	  
	  /** Class constructor.
	   */
	  public KeyDataEntry( double key, TID tid) {
	     this.key = new DoubleKey(key); 
	     this.data = new LeafData(tid);
	  };
	  
	  public KeyDataEntry( double key, LID lid) {
		     this.key = new DoubleKey(key); 
		     this.data = new LeafDataL(lid);
		  };

  /** Class constructor.
   */
  public KeyDataEntry( KeyClass key, TID tid){
     data = new LeafData(tid); 
     if ( key instanceof IntegerKey ) 
        this.key= new IntegerKey(((IntegerKey)key).getKey());
     else if ( key instanceof StringKey ) 
        this.key= new StringKey(((StringKey)key).getKey()); 
     else if (key instanceof DoubleKey)
    	 this.key= new DoubleKey(((DoubleKey)key).getKey()); 	 
  };
  
  public KeyDataEntry( KeyClass key, LID lid){
	     data = new LeafDataL(lid); 
	     if ( key instanceof IntegerKey ) 
	        this.key= new IntegerKey(((IntegerKey)key).getKey());
	     else if ( key instanceof StringKey ) 
	        this.key= new StringKey(((StringKey)key).getKey());    
	     else if (key instanceof DoubleKey)
	    	this.key= new DoubleKey(((DoubleKey)key).getKey()); 
	  };


  /** Class constructor.
   */
  public KeyDataEntry( String key, TID tid) {
     this.key = new StringKey(key); 
     this.data = new LeafData(tid);
  }; 
  
  public KeyDataEntry( String key, LID lid) {
	     this.key = new StringKey(key); 
	     this.data = new LeafDataL(lid);
	  }; 

  /** Class constructor.
   */
  public KeyDataEntry( KeyClass key,  DataClass data) {
     if ( key instanceof IntegerKey ) 
        this.key= new IntegerKey(((IntegerKey)key).getKey());
     else if ( key instanceof StringKey ) 
        this.key= new StringKey(((StringKey)key).getKey()); 
     else if (key instanceof DoubleKey)
    	 this.key= new DoubleKey(((DoubleKey)key).getKey());
     if ( data instanceof IndexData ) 
        this.data= new IndexData(((IndexData)data).getData());
     else if ( data instanceof LeafData ) 
        this.data= new LeafData(((LeafData)data).getData()); 
     else if (data instanceof LeafDataL)
    	 this.data = new LeafDataL(((LeafDataL)data).getData());
  }

  /** shallow equal. 
   *  @param entry the entry to check again key. 
   *  @return true, if entry == key; else, false.
   */
  public boolean equals(KeyDataEntry entry) {
      boolean st1,st2;

      if ( key instanceof IntegerKey )
         st1= ((IntegerKey)key).getKey().equals
                  (((IntegerKey)entry.key).getKey());
      else if(key instanceof StringKey)
         st1= ((StringKey)key).getKey().equals
                  (((StringKey)entry.key).getKey());
      else 
    	 st1= ((DoubleKey)key).getKey() == ((DoubleKey)entry.key).getKey(); 

      if( data instanceof IndexData )
         st2= ( (IndexData)data).getData().pid==
              ((IndexData)entry.data).getData().pid ;
      else if(data instanceof LeafData)
    	  st2 = ((TID)((LeafData)data).getData()).equals(((TID)((LeafData)entry.data).getData()));
      else 
         st2= ((LID)((LeafDataL)data).getData()).equals(((LID)((LeafDataL)entry.data).getData()));

  
      return (st1&&st2);
  }     
}

